<?php
class msurat extends CI_Model{
	var $surat = "m_surat";

	function __construct()
	{
		parent::__construct();
	}
	
	function setData($id_surat,$baris_pertama,$baris_kedua,$id_sektor,$alamat_surat)
	{
		$this->id_surat= $id_surat;
		$this->baris_pertama= $baris_pertama;
		$this->baris_kedua= $baris_kedua;
		$this->id_sektor= $id_sektor;
		$this->alamat_surat= $alamat_surat;
		
	}

	function getList($page){
		$strSql = "
			select sr.*,skt.name as nama_sektor
			from m_surat sr
			inner join(
			  select kepolisian.id,kepolisian.name
			  from (
				select rs.id_resort as id, rs.nama_resort as name
				from m_resort rs
				union all
				select sk.id_sektor as id, sk.nama_sektor as name
				from m_sektor sk
			  ) kepolisian
			) skt on sr.id_sektor = skt.id
			limit ".$page."
		";
		$query = $this->db->query($strSql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function create()
	{		
		$arrayData = array(
			'id_surat'=>$this->id_surat,
			'baris_pertama'=>$this->baris_pertama,
			'baris_kedua'=>$this->baris_kedua,
			'id_sektor'=>$this->id_sektor,
			'alamat_surat'=>$this->alamat_surat
		);
		return $this->db->insert($this->surat, $arrayData);
	}
	
	function update($id_surat)
	{
		$arrayData = array(
			'baris_pertama'=>$this->baris_pertama,
			'baris_kedua'=>$this->baris_kedua,
			'id_sektor'=>$this->id_sektor,
			'alamat_surat'=>$this->alamat_surat
		);
		$this->db->where('id_surat', $id_surat);
		return $this->db->update($this->surat, $arrayData);
	}
        
	function remove($id_surat)
	{
		$this->db->where('id_surat', $id_surat);
		return $this->db->delete($this->surat);
	}	
	
	function detail($id_surat)
	{
		$this->db->where('id_surat', $id_surat);
		$query = $this->db->get($this->surat);	
		return $query->result_array();
	}
	
	function getDetSektor($id_sektor)
	{
		$strSql = "
			select sr.*,skt.name as nama_sektor
			from m_surat sr
			inner join(
			  select kepolisian.id, kepolisian.name
			  from (
				select rs.id_resort as id, rs.nama_resort as name
				from m_resort rs
				union all
				select sk.id_sektor as id, sk.nama_sektor as name
				from m_sektor sk
			  ) kepolisian
			) skt on sr.id_sektor = skt.id
			where sr.id_sektor = ".$id_sektor."
		";
		$query = $this->db->query($strSql);
		return $query->result_array();
	}
	
	function getListSearch($name){
		$strSql = "
			select sr.*,skt.name as nama_sektor
			from m_surat sr
			inner join(
			  select kepolisian.id,kepolisian.name
			  from (
				select rs.id_resort as id, rs.nama_resort as name
				from m_resort rs
				union all
				select sk.id_sektor as id, sk.nama_sektor as name
				from m_sektor sk
			  ) kepolisian
			) skt on sr.id_sektor = skt.id
			where skt.name like '%".$name."%'
		";
		$query = $this->db->query($strSql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
}
?>